package cn.layfolk.seckill.mq.listener;

import cn.hutool.json.JSONUtil;
import cn.layfolk.daimao.consts.MQConstans;
import cn.layfolk.seckill.mq.msg.DelayOrderMessage;
import cn.layfolk.seckill.service.TOrderInfoService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@RocketMQMessageListener(
        topic = MQConstans.SECKILL_ORDER_TOPIC,
        consumerGroup = MQConstans.DELAY_ORDER_GROUP,
        selectorExpression = MQConstans.DELAY_ORDER_TAG
)
@Component
public class DelayOrderMQListener implements RocketMQListener<DelayOrderMessage> {

    private final Logger log = LoggerFactory.getLogger("DELAY_ORDER_MQ_LISTENER");
    @Autowired
    private TOrderInfoService orderInfoServicel;


    @Override
    public void onMessage(DelayOrderMessage msg) {
        log.info("收到延迟消息：{}", JSONUtil.toJsonStr(msg));
        // 检查订单是否已支付，如果未支付需要实现库存回补
        orderInfoServicel.checkOrderTimeout(msg.getOrderNo(),msg.getSeckillId());

    }
}
